#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Author          : ricsy
@File            : sphinx_toc_tree_plus.py
@Create Time     : 2025/6/8 06:02
@Last Modified   :
@Description     : toctree 功能增强插件，标题支持变量替换
"""
from docutils.parsers.rst import directives

from extensions.common import process_caption
from sphinx.application import Sphinx
from sphinx.directives.other import TocTree


class TocTreePlus(TocTree):
    def run(self):
        if "caption" in self.options:
            self.options["caption"] = process_caption(self, self.options["caption"])
        nodes = super().run()
        return nodes


# noinspection PyUnusedLocal
def setup(app: Sphinx):
    directives.register_directive("toctree7", TocTreePlus)

    return {
        "version": "0.1",
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }
